/**
 * 每日礼包 单项
 */
class DaygiftItem extends ItemRendererBase {

    public constructor() {
        super();
        this.skinName = DaygiftItemSkin;
    }



    // 按钮
    private btnTap: eui.Button;
    // 标题
    private labTitle: eui.Label;
    // 奖励1
    private grpTool1: eui.Group;
    // 奖励2
    private grpTool2: eui.Group;


    protected dataChanged() {
        // 显示第一个奖励
        if (this.data.list[0].type == 1) {
            this.grpTool1.$children[1]["source"] = "plant_" + this.data.list[0].pid + "_png";
        } else {
            this.grpTool1.$children[1]["scaleX"] = 1;
            this.grpTool1.$children[1]["scaleY"] = 1;
            this.grpTool1.$children[1]["source"] = "tool_muck_png";
        }

        // 如果两个奖励 显示第二个
        if (this.data.list.length === 2) {
            this.grpTool2.visible = true;
            if (this.data.list[1].type == 1) {
                this.grpTool2.$children[1]["source"] = "plant_" + this.data.list[1].pid + "_png";
            } else {
                this.grpTool2.$children[1]["scaleX"] = 1;
                this.grpTool2.$children[1]["scaleY"] = 1;
                this.grpTool2.$children[1]["source"] = "tool_muck_png";
            }
        }

        this.labTitle.text = "邀请" + this.data.num + "人\n可得";
        this.btnTap.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.wxshare, this);

        // 处理按钮状态
        this.btnStatus();
        // 监听今日邀请数量改变
        EventManager.instance.addEvent(EventName.HAS_GIFT_CHANGE, this.btnStatus, this, true);
    }



    /**
     * 分享
     */
    private wxshare() {
        platform.wxshare(1, (e) => { })
    }


    /**
     * 领取
     */
    private getGift() {
        // 判断数量
        if (PlayerSelfData.gift < this.data.num) {
            SceneManager.ins.alert("人数不够哟");
            return;
        } else {
            // 判断领取
            for (var i = 0; i < PlayerSelfData.has_gift.length; i++) {
                if (PlayerSelfData.has_gift[i] == this.data.id) {
                    SceneManager.ins.alert("今天已领取过啦");
                    return;
                }
            }
        }
        // 发送领取请求
        Net.ins.send(['getGift'], { "type": this.data.id }, (e) => {
            SceneManager.ins.alert("领取成功");
            // 加上
            CommonData.plantInfo((e) => {
                e[1]['i_have'] = e[1]['i_have'] + 1;
                CommonData.setplantInfo(e[1]['id'], e[1]);
            })
            PlayerSelfData.has_gift.push(this.data.id)
            EventManager.instance.dispatch(EventName.HAS_GIFT_CHANGE, PlayerSelfData.has_gift);
        });
    }


    /**
     * 处理按钮状态
     */
    private btnStatus() {
        this.btnTap.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.getGift, this)
        this.btnTap.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.wxshare, this)
        // 判断数量
        if (PlayerSelfData.gift < this.data.num) {
            this.btnTap.label = "去分享";
            this.btnTap.addEventListener(egret.TouchEvent.TOUCH_TAP, this.wxshare, this);
        } else {
            this.btnTap.label = "领取";
            this.btnTap.addEventListener(egret.TouchEvent.TOUCH_TAP, this.getGift, this);
            for (var i = 0; i < PlayerSelfData.has_gift.length; i++) {
                if (PlayerSelfData.has_gift[i] == this.data.id) {
                    this.btnTap.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.getGift, this)
                    this.btnTap.label = "已领取";
                    this.btnTap.enabled = false;
                    break;
                }
            }
        }
    }


}